<template>
    <el-row>
        <el-row class="title">
            <h3>直播banner</h3>
        </el-row>
        <el-row>
            <el-form :inline="true">
                <el-form-item>
                    <el-button type="primary" @click="showAddDialog"><i class="el-icon-plus el-icon--left"></i>添加banner</el-button>
                </el-form-item>
                <el-form-item label="搜索">
                    <el-input class="search-input" v-model="bannerNameLike" placeholder="请输入banner名称" clearable @change="getBanner"></el-input>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row>
            <el-table :data="datas" border>
                <el-table-column prop="bannerId" label="id" align="center"></el-table-column>
                <el-table-column prop="bannerName" label="名称" align="center"></el-table-column>
                <el-table-column prop="remark" label="备注" align="center"></el-table-column>
                <el-table-column label="跳转地址" align="center">
                    <template slot-scope="scope">{{scope.row.jumpUrl ? scope.row.jumpUrl : '无跳转'}}</template>
                </el-table-column>
                <el-table-column prop="addTime" label="添加时间" align="center">
                    <template slot-scope="scope">{{moment(scope.row.addTime).format('YYYY-MM-DD HH:mm:ss')}}</template>
                </el-table-column>
                <el-table-column label="状态" align="center" width="80px">
                    <template slot-scope="scope">{{scope.row.display ? '显示' : '隐藏'}}</template>
                </el-table-column>
                <el-table-column prop="orderNumber" label="排序值" align="center" width="80px"></el-table-column>
                <el-table-column label="图片" align="center" width="240px">
                    <template slot-scope="scope">
                        <img class="banner-thumb" :src="getImageUrl(scope.row.bannerImageUrl)" alt="">
                    </template>
                </el-table-column>
                <el-table-column label="视频地址" align="center" width="240px">
                    <template slot-scope="scope">
                    	<video :src="getImageUrl(scope.row.bannerVideoUrl)" width="100%" controls></video><br>
                        <a :href="getImageUrl(scope.row.bannerVideoUrl)" target="_blank">点击打开视频</a>
                    </template>
                </el-table-column>
                </el-table-column>
                <el-table-column label="操作" align="center" width="160px">
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" @click="updateDatas(scope.$index, scope.row)">修改</el-button>
                        <el-button size="small" type="danger" @click="deleteDatas(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
        <el-dialog :title="add?'添加banner':'修改banner'" :visible.sync="showDialog" width="600px" @close="closeVideoDialog">
            <el-form :model="dialogData" label-width="120px">
                <el-form-item label="banner名称">
                    <el-input v-model="dialogData.bannerName" placeholder="请输入banner名称"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="dialogData.remark" placeholder="请输入备注"></el-input>
                </el-form-item>
                <!-- <el-form-item label="是否跳转">
                    <el-switch v-model="jump" active-text="是" inactive-text="否"></el-switch><el-input v-if="jump" v-model="dialogData.jumpUrl" placeholder="请输入跳转地址"></el-input>
                </el-form-item> -->
                <el-form-item label="banner图片">
                    <el-upload class="avatar-uploader" :headers="uploadHeader" :action="BASEURL + '/upload/single/banner'" :show-file-list="false" :on-success="handleBannerSuccess" accept="image/*" :on-progress="uploadImageProgress">
                        <el-button size="small" type="primary" :loading="imageLoading">上传图片</el-button>
                    </el-upload>
                    <el-progress v-if="imageLoading == true" :percentage="imageUploadPercent"></el-progress>
                    <div v-if="imageUrl">
                        <img :src="getImageUrl(imageUrl)" alt="" class="banner-thumb">
                    </div>
                </el-form-item>
                <el-form-item label="视频">
                    <el-upload :headers="uploadHeader" :action="BASEURL + '/upload/single/video'" :show-file-list="false" :on-success="uploadVideoSuccess" accept="video/*" :on-progress="uploadVideoProgress">
                        <el-button size="small" type="primary" :loading="videoLoading">上传视频</el-button>
                    </el-upload>
                    <el-progress v-if="videoLoading == true" :percentage="videoUploadPercent"></el-progress>
                    <div v-if="videoUrl">
                    	<video :src="getImageUrl(videoUrl)" width="100%" controls></video>
                    </div>
                </el-form-item>
                <el-form-item label="是否显示">
                    <el-switch v-model="dialogData.display" active-text="是" inactive-text="否"></el-switch>
                </el-form-item>
                <el-form-item label="排序值">
                    <el-input-number v-model="dialogData.orderNumber" :min="1" label="排序值"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelAddVideos">取 消</el-button>
                <el-button type="primary" @click="addVideos" :loading="addLoading">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 分页 -->
        <el-row>
            <el-col class="pagination">
                <el-pagination @current-change="currentChange" :current-page="pageId" :page-size="pageSize" layout="total, prev, pager, next" :total="counts">
                </el-pagination>
            </el-col>
        </el-row>
    </el-row>
</template>
<script>
import {
    getBannerLists,
    addBanner,
    deleteBannerById,
    updateBannerById
} from '@/api/api'
export default {
    data: function() {
        return {
            pageId: 1,
            pageSize: 10,
            counts: 0,
            add: true,
            bannerNameLike: '',
            showDialog: false,
            addLoading: false,
            datas: [],
            dialogData: {
                bannerId: null,
                bannerName: '',
                bannerImageUrl: '',
                bannerVideoUrl: '',
                jumpUrl: '',
                remark: '',
                orderNumber: 1,
                display: true
            },
            uploadHeader: {
                TOKEN: sessionStorage.getItem('jwt')
            },
            imageUrl: '',
            videoUrl: '',
            jump: false,
            videoUploadPercent: 0,
            videoLoading: false,
            imageLoading: false,
            imageUploadPercent: 0
        }
    },
    created: function() {
        this.pageId = parseInt(this.$route.query.page) || 1;
        this.getBanner();
    },
    methods: {
        getBanner: function() {
            getBannerLists({ params: { pageId: this.pageId, pageSize: this.pageSize, bannerNameLike: this.bannerNameLike } }).then(res => {
                console.log(res)
                this.counts = res.count;
                this.datas = res.list
            })
        },
        showAddDialog: function() {
            this.add = true;
            this.showDialog = true;
        },
        closeVideoDialog: function() {
            this.add = true;
            this.showDialog = false;
            this.dialogData = {
                bannerId: null,
                bannerName: '',
                bannerImageUrl: '',
                bannerVideoUrl: '',
                jumpUrl: '',
                remark: '',
                orderNumber: 1,
                display: true
            }
            this.jump = false;
            this.imageUrl = '';
            this.videoUrl = '';
        },
        updateDatas: function(index, row) {
            this.dialogData = {
            	bannerId: row.bannerId,
                bannerName: row.bannerName,
                bannerImageUrl: row.bannerImageUrl,
                bannerVideoUrl: row.bannerVideoUrl,
                jumpUrl: row.jumpUrl,
                remark: row.remark,
                orderNumber: row.orderNumber,
                display: row.display
            }
            this.jump = !!row.jumpUrl
            this.imageUrl = row.bannerImageUrl;
            this.videoUrl = row.bannerVideoUrl;
            this.add = false;
            this.showDialog = true;
        },
        deleteDatas: function(index, row) {
            this.$confirm('是否删除?', '删除banner', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'danger'
            }).then(() => {
                deleteBannerById(row.bannerId).then(res => {
                    this.getBanner();
                    this.$message({
                        type: 'success',
                        message: '删除成功'
                    })
                    this.showDialog = false;
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        cancelAddVideos: function() {
            this.closeVideoDialog()
        },
        addVideos: function() {
            if (this.add) {
                addBanner(this.dialogData).then(res => {
                    console.log(res)
                    this.getBanner();
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    })
                    this.closeVideoDialog()
                })
            } else {
                updateBannerById(this.dialogData.bannerId, this.dialogData).then(res => {
                    this.getBanner();
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    })
                    this.closeVideoDialog()
                })
            }
        },
        currentChange: function(val) {
            this.$router.push('?page=' + val)
            this.pageId = val;
            this.getBanner()
        },
        handleBannerSuccess(res) {
            if (res.status) {
                this.$message({
                    message: '上传成功',
                    type: 'success'
                })
                this.imageUrl = res.data.webPath;
                this.dialogData.bannerImageUrl = res.data.webPath;
            } else {
                this.$message({
                    message: '上传失败',
                    type: 'error'
                })
            }
            this.imageLoading = false;
        },
        uploadVideoSuccess: function(res) {
            if (res.status) {
                this.$message({
                    message: '上传成功',
                    type: 'success'
                })
                this.videoUrl = res.data.webPath;
                this.dialogData.bannerVideoUrl = res.data.webPath;
            } else {
                this.$message({
                    message: '上传失败',
                    type: 'error'
                })
            }
            this.videoLoading = false;
        },
        uploadVideoProgress: function(event, file, fileList){
            this.videoLoading = true;
            this.videoUploadPercent = +file.percentage.toFixed(0);
        },
        uploadImageProgress: function(event, file, fileList){
            this.imageLoading = true;
            this.imageUploadPercent = +file.percentage.toFixed(0);
        }
    }
}
</script>
<style scoped lang="scss">
.title {
    font-size: 12px;
    margin-bottom: 5px;
    h3 {
        display: inline-block;
        padding: 5px 0;
        margin: 0;
        font-weight: normal;
        color: #000;
        border-bottom: 2px solid #000;
    }
}

.banner-thumb {
    width: 100%;
}

.thumb {
    width: 120px;
}
</style>